import numpy as np
import scipy.io as scio
import matplotlib.pyplot as plt

def txt2arr(txt):
	arr = []
	with open(txt, 'r') as f:#with语句自动调用close()方法
		for line in f.readlines():
			# print(line)
			num = int(line)
			arr.append(num)
	arr = np.array(arr)
	# print(arr.shape)
	# print(arr[:9])
	return arr


def txt2arr2(txt):
	arr1 = []
	arr2 = []
	with open(txt, 'r') as f:#with语句自动调用close()方法
		for line in f.readlines():
			# print(line)
			line_int, line_float = line.replace('\n', '').split('\t')
			# print(line_int, ',', line_float)
			num = int(line_int)
			arr1.append(num)
			num = float(line_float)
			arr2.append(num)
	arr1 = np.array(arr1).reshape(-1)
	arr2 = np.array(arr2).reshape(-1)
	# print(arr.shape)
	# print(arr[:9])
	return arr1, arr2


import argparse
parser = argparse.ArgumentParser(description="Argument")
parser.add_argument('--txt', type=str, default='kt.txt', help='')
args = parser.parse_args()


def main():

	print('\n\n\n\t', 'Drawing K-th & Time')
	print('*'*64)

	# arr_k = txt2arr('k.txt')
	arr1, arr2 = txt2arr2(args.txt)
	arr1.sort()
	arr2.sort()
	arr1 = arr1[::-1]
	arr2 = arr2[::-1]
	# plt.subplot(121),plt.plot(arr1),plt.title('K')#,plt.ylabel('log')
	# plt.subplot(122),plt.plot(arr2),plt.title('Time(us)')#,plt.ylabel('log(us)')
	# # plt.legend(['K', 'MS'])
	# plt.show()

	fig = plt.figure() 
	ax1 = fig.add_subplot(1,2,1)
	# ax1.plot(arr1)
	ax1.scatter(range(arr1.shape[0]), arr1, marker='.', s=.1)
	ax1.set_yscale('log')
	ax1.set_title('K')
	
	ax2 = fig.add_subplot(1,2,2)
	# ax2.plot(arr2)
	ax2.scatter(range(arr2.shape[0]), arr2, s=1)
	ax2.set_yscale('log')
	ax2.set_title('Time(us)')

	plt.show()


if __name__ == '__main__':
	
	arr1, arr2 = txt2arr2(args.txt)
	scio.savemat('kt.mat', {'t':arr2, 'k':arr1})

